<?php
class Application_Model_Base_Memory implements Application_Model_Base_Interface
{
	public static $_stores = array();

	protected $_name = 'groups';
	
	//protected $_schema = new Application_Model_Base_Memory_Schema(array());
	public static $sortField = "";
	public static $sortOrder = 1;

	public function __construct()
	{
		if(! isset(self::$_stores[$this->_name]))
		{
			self::$_stores[$this->_name] = array();
		}
	}

	public function update($ids, $values)
	{
		$values = Application_Model_Base_Memory_Schema_MakeUp::
			makeUpField($this->_schema->getSchema(), $values);
		
		if(!is_array($ids))
		{
			$ids = array($ids);
		}

		foreach ($ids as $id)
		{
			if (isset(self::$_stores[$this->_name][$id]))
			{
				$currentValue = self::$_stores[$this->_name][$id];

				foreach ($values as $key => $value)
				{
					$currentValue[$key] = $value;
				} 
				self::$_stores[$this->_name][$id] = $values;
			}
		}
	}

	public function create($values)
	{
		$id = rand(1, 1000000);
		
		$values = Application_Model_Base_Memory_Schema_MakeUp::
			makeUpField($this->_schema->getSchema(), $values);
		
		if (Application_Model_Base_Memory_Schema_MakeUp::
				makeUpField($this->_schema->getSchema(), $values) == false)
		{
			return false;
		}
		
		if(isset(self::$_stores[$this->_name][$id]))
		{
			return $this->create($values);
		}
		else
		{
			return self::$_stores[$this->_name][$id] = $values;
		}
		return $id;
	}

	public function remove($ids)
	{
		if(is_int($ids))
		{
			$ids = array($ids);
		}

		foreach ($ids as $id)
		{
			unset(self::$_stores[$this->_name][$id]);
		}
	}

	public function search($arguments, $offset = 0, $limit = null,
			$order = null, $count = false)
	{
		$_count = 0;

		$returnArray = array();

		foreach (self::$_stores[$this->_name] as $index => $value)
		{
			if(Application_Model_Base_Memory_CheckArguments::
					checkArg($arguments, $value) == true)
			{
				$returnArray[count($returnArray)] = $index;
			}
		}

		if($count == true)
		{
			return count($returnArray);
		}

		$this->_orderArray($returnArray, $order);

		$someArray = array();

		if($limit == null)
		{
			$limit = count($returnArray);
		}

		if ($limit + $offset > count($returnArray))
		{
			for($i = $offset; $i < count($returnArray); $i++)
			{
				$someArray[count($someArray)] = $returnArray[$i];
			}

			return $someArray;
		}
		else
		{
			for($i = $offset; $i < $limit + $offset ; $i++)
			{
				$someArray[$i] = $returnArray[$i];
			}
			return $someArray;
		}

		return $returnArray;
	}

	private function _orderArray($someArray, $order)
	{
	}

	public function read($ids, $fields = array())
	{
		$returnArray = Array();

		if(is_int($ids))
		{
			$ids = array($ids);
		}

		foreach ($ids as $id)
		{
			if(isset(self::$_stores[$this->_name][$id])){
				$currentValue = self::$_stores[$this->_name][$id];

				$currentReturnValue = Array();

				if($fields == null)
				{
					foreach ($currentValue as $index => $value)
					{
						$currentReturnValue[$index] = $currentValue[$index];
					}
				}
				if($fields!=null)
				{
					foreach ($fields as $index)
					{
						$currentReturnValue[$index] = $currentValue[$index];
					}
				}
				$returnArray[count($returnArray)] = $currentReturnValue;
			}
		}
		return $returnArray;
	}

}
